/*******************************************************************************
*  file    : find_patterns.hpp
*  created : 28.08.2014
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#ifndef FIND_PATTERNS_HPP
#define FIND_PATTERNS_HPP

#include <array>
#include <vector>
#include <complex>
#include <QDebug>

namespace signal_utils
{
inline std::array<size_t,4> find_patterns(const std::vector<std::complex<double> >& data)
{
    std::array<size_t,4> res = {0,0,0,0};

    size_t start = 0;
    while(data[start++].real() == 0) {}
    double k = 0;
    while( (start < data.size()-1) && std::abs(k) < 50.)
        {
            k = (data[start+1].real() - data[start].real() );
            start++;
        }

    res[0] = start - 2;
    start += 126;  //HACK: магическое число, зависящее от кол-ва импкльзов в посылке.
    res[1] = start;

    auto max_el = std::max_element(data.begin() + res[1] + 500, data.end(),
            [](const std::complex<double>& d1,const std::complex<double>& d2){return (d1.real() < d2.real());});
    if(max_el != data.end())
        {
            res[2] = std::distance(data.begin(),max_el) - 150;
            res[3] = std::distance(data.begin(),max_el) + 100;
        }

    return res;
}

}//namespace signal_utils

#endif // FIND_PATTERNS_HPP
